package com.company.cloud.club.modules.db.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.company.cloud.club.modules.db.domain.Praise;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * *****************************************************************************
 * <p>
 * 功能名           ：com.company.cloud.club.modules.db.dao
 * 系统名           ：
 * <p>
 * *****************************************************************************
 * Modification History
 * <p>
 * Date        Name                    Reason for Change
 * ----------  ----------------------  -----------------------------------------
 * 2020/7/18     zhangxu                 Created
 */
public interface PraiseMapper extends BaseMapper<Praise> {

    @Select("SELECT * FROM club_praise WHERE comment_id = #{commentId, jdbcType=VARCHAR} " +
            "AND user_id = #{userId, jdbcType=INTEGER} ORDER BY time_created DESC LIMIT 0, 1")
    Praise selectByCommentIdAndUserId(@Param("commentId") String commentId,
                                      @Param("userId") Integer userId);

    @Select("SELECT count(*) FROM club_praise WHERE comment_id = #{commentId, jdbcType=VARCHAR}")
    Integer countByCommentId(@Param("commentId") String commentId);

    @Update("UPDATE club_recommend_card SET praise_num = praise_num + 1 " +
            "WHERE id = #{commentId, jdbcType=VARCHAR}")
    void addPraise(@Param("commentId") String commentId);

    @Update("UPDATE club_recommend_card SET praise_num = praise_num - 1 " +
            "WHERE id = #{commentId, jdbcType=VARCHAR}")
    void subPraise(@Param("commentId") String commentId);

    @Delete("DELETE FROM club_praise WHERE id = #{id, jdbcType=VARCHAR}")
    void deletePraise(@Param("id") String id);

}
